# -*- coding: utf-8 -*-
# @File     : permissions.py
# @Author   : bingjia
# @Time     : 2020/8/4 17:08
# @Desc     : 权限

from django.apps import apps

from ..base.api import permissions
from . import services


class HasProjectPerm(permissions.PermissionComponent):
    def __init__(self, perm, *components):
        self.project_perm = perm
        super().__init__(*components)

    def check_permissions(self, request, view, obj=None):
        return services.user_has_perm(request.user, self.project_perm, obj)


class IsProjectAdmin(permissions.PermissionComponent):
    """
    是否是项目管理员
    """

    def _get_object_project(self, obj):
        """
        获取项目
        """
        project = None
        Project = apps.get_model("projects", "Project")
        if isinstance(obj, Project):
            project = obj
        elif obj and hasattr(obj, 'project'):
            project = obj.project
        return project

    def check_permissions(self, request, view, obj=None):
        if request.user.is_superuser:
            return True

        if isinstance(obj, list):
            for i in obj:
                project = self._get_object_project(i)
                if project is None:
                    return False
                (membership, _) = apps.get_model("projects", "Membership").get_user_project_membership(None,
                                                                                                       project=project,
                                                                                                       user=request.user)
                if membership is None:
                    return False
            return True

        elif isinstance(obj, object):
            project = self._get_object_project(obj)
            if project is None:
                return False

            (membership, _) = apps.get_model("projects", "Membership").get_user_project_membership(None,
                                                                                                   project=project,
                                                                                                   user=request.user)
            if membership and membership.is_admin:
                return True

        return False


class IsProjectMembership(permissions.PermissionComponent):
    """
    是否是项目成员
    """

    def _get_object_project(self, obj):
        """
        获取项目
        """
        project = None
        Project = apps.get_model("projects", "Project")
        if isinstance(obj, Project):
            project = obj
        elif obj and hasattr(obj, 'project'):
            project = obj.project
        return project

    def check_permissions(self, request, view, obj=None):
        if request.user.is_superuser:
            return True

        if isinstance(obj, list):
            for i in obj:
                project = self._get_object_project(i)
                if project is None:
                    return False
                (membership, _) = apps.get_model("projects", "Membership").get_user_project_membership(None,
                                                                                                       project=project,
                                                                                                       user=request.user)
                if membership is None:
                    return False
            return True

        elif isinstance(obj, object):
            project = self._get_object_project(obj)
            if project is None:
                return False

            (membership, _) = apps.get_model("projects", "Membership").get_user_project_membership(None,
                                                                                                   project=project,
                                                                                                   user=request.user)
            if membership:
                return True

        return False
